package com.h3c.ptability.mapstruct;

import com.h3c.ptability.bo.PtEsignRecordBO;
import com.h3c.ptability.cache.DeptCache;
import com.h3c.ptability.cache.PtEnumCache;
import com.h3c.ptability.entity.PtEsignRecord;
import com.h3c.ptability.enums.ElectronicSignRecordStatus;
import com.h3c.ptability.vo.PtEsignRecordDownloadVO;
import com.h3c.ptability.vo.PtEsignRecordVO;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author chenyifan
 * @date 2022/5/31 16:35
 */
@Mapper(uses = DateMapper.class)
public interface PtEsignRecordMapstruct {

    PtEsignRecordMapstruct INSTANCE = Mappers.getMapper(PtEsignRecordMapstruct.class);

    /**
     * 实体类列表转成VO列表
     *
     * @param recordList
     * @return
     */
    List<PtEsignRecordVO> entityListToVoList(List<PtEsignRecord> recordList);

    /**
     * 实体类列表转成VO列表
     *
     * @param recordList
     * @return
     */
    List<PtEsignRecordDownloadVO> entityListToDownloadVoList(List<PtEsignRecord> recordList);

    /**
     * 实体类转成BO
     *
     * @param esignRecord
     * @return
     */
    @Mappings({
            @Mapping(source = "signStatus.statusName", target = "signStatusName")
    })
    PtEsignRecordBO entityToBO(PtEsignRecord esignRecord);

    /**
     * 填充属性
     *
     * @param voList
     */
    @AfterMapping
    default void afterEntityList2VoList(@MappingTarget List<PtEsignRecordVO> voList) {
        voList.forEach(s -> {
            if (s.getSignStatus().equals(ElectronicSignRecordStatus.SIGNING) ||
                    s.getSignStatus().equals(ElectronicSignRecordStatus.SIGN_SUCCESS) ||
                    s.getSignStatus().equals(ElectronicSignRecordStatus.WAIT_ORG_SIGN)) {
                s.setCanCancelSign(true);
            } else {
                s.setCanCancelSign(false);
            }
            if (!StringUtils.hasText(s.getSignFlowId()) || s.getSignFlowId().equals(PtEsignRecord.ALL_ORG_SIGN)) {
                s.setCanCancelSign(false);
            }
        });
    }

    /**
     * 填充属性
     *
     * @param voList
     */
    @AfterMapping
    default void afterEntityList2DownloadVoList(@MappingTarget List<PtEsignRecordDownloadVO> voList) {
        voList.forEach(s -> {
            s.setSceneType(PtEnumCache.getEnumDataInRedis("ptability-manage", "ES_SCENE_TYPE", s.getSceneType()))
                    .setSignStatusName(s.getSignStatus().getStatusName())
                    .setDept1IdName(Objects.nonNull(s.getDept1id()) ? DeptCache.getPtDeptByDeptId(s.getDept1id()).getDeptName() : "")
                    .setDept2IdName(Objects.nonNull(s.getDept2id()) ? DeptCache.getPtDeptByDeptId(s.getDept2id()).getDeptName() : "")
                    .setDept3IdName(Objects.nonNull(s.getDept3id()) ? DeptCache.getPtDeptByDeptId(s.getDept3id()).getDeptName() : "");
        });
    }
}
